<?php


namespace frontend\controllers\cyf;


use frontend\controllers\common;
use frontend\models\cyf\MemberCard;
use frontend\models\cyf\User;
use logic\cyf\VipLogic;
use servers\cyf\PictiueFilter;
use servers\cyf\TextFilter;
use servers\cyf\UseFilter;
use yii\web\Controller;
use yii\web\Request;
use yii\web\UploadedFile;

class VipController extends Controller
{
    /**
     * 查看用户会员权益
     * @param Request $request
     * @return array|false|string
     */
    public function actionIndex(Request $request)
    {
        if(!$request->isGet) common::fail('10001','null','请求方式错误');
        try {
            $uid = $request->get('id');
            if(empty($uid) || !is_numeric($uid)) return common::fail('10001','null','参数错误');
            $data =  VipLogic::detail($uid);
            return  common::success('0',$data,'操作成功');
        }catch (\Exception $exception){
            return  common::fail('10005','null',$exception->getMessage());
        }
    }

    /**
     * 降级
     * @param Request $request
     * @return array|false|string
     * @name cyf
     */
    public function actionDowngrad(Request $request)
    {
        if(!$request->isPost) common::fail('10001','null','请求方式错误');
        try {
            $uid = $request->post('uid');
            if(empty($uid) || !is_numeric($uid)) return common::fail('10001','null','参数错误');
             $res = VipLogic::downGrad($uid);
            if(!$res) return common::fail('60001','null','操作失败');
            return common::success('60001','null','操作成功');
        }catch (\Exception $exception){
            return common::fail('60001','null',$exception->getMessage());
        }
    }

    /**
     * 增加积分
     * @param Request $request
     * @return array|false|string
     * @name cyf
     */
    public function actionGetIntegral(Request $request)
    {
        if(!$request->isPost) common::fail('10001','null','请求方式错误');
        try {
            $params = $request->post();
            if(empty($params['uid']) || !is_numeric($params['uid'])) return common::fail('10001','null','参数错误');
            if(empty($params['integral']) || !is_numeric($params['integral'])) return common::fail('10001','null','参数错误');
            $res = VipLogic::getIntegral($params);
            if(!$res) return common::fail('60002','null','操作失败');
            return common::success('0',$res,'操作成功');
        }catch (\Exception $exception){
            return common::fail('60003','null',$exception->getMessage());
        }
    }


    /**
     * 使用策略模式
     * @param Request $request
     */
    public function actionAdd(Request $request)
    {
        $path = UploadedFile::getInstancesByName('pic')[0];
        $filter = new UseFilter(new PictiueFilter());
        $res = $filter->filterContent($path->tempName);
        var_dump($res);die();
    }

}